/*
 * Takes a character, converts it to hex, decimal, binary, octal and html. 
 * Then wraps each of the fields with XML and sends it back through the response.
 */
package com.mcv.servlet;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.awt.image.ImageFormatException;

import com.icesoft.AppState;
import com.icesoft.x.demo.ApplicationState;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.faces.el.ValueBinding;

public class HttpTunnelingServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(HttpTunnelingServlet.class);
	

public void doPost(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
    {
        HttpSession session = request.getSession();
        AppState state = AppState.getInstance();
     
      
        
        
// Get the content type
        String contentType = request.getContentType();
        String a = request.getQueryString();
        DataInputStream in =null;
        int pos = a.lastIndexOf("apns");
        if (pos < 1 ) {
        	// Get the input stream to read the uploaded file
            ServletInputStream servIn = request.getInputStream();
            in = new DataInputStream(servIn);
         // Create a decoder for JPEG data streams
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);

    // Decode the JPEG
            BufferedImage image = decoder.decodeAsBufferedImage();
            
            byte[] imageBytes = convertJpgToBytes(image);
            AppState appState = AppState.getInstance();
            appState.setSessionCount(imageBytes,request.getQueryString());
               
        } else {
        

        ServletInputStream servIn = request.getInputStream();
        in = new DataInputStream(servIn);
        String line;
        String dataLines = null;
       while ((line = in.readLine()) != null)
    	   
        {
    	 line =  line.replaceFirst("<", "");
    	 line =   line.replaceFirst(">", "");
    	   System.out.println("datalines="+line);
    	   dataLines = line;
            if (line.trim().length() == 0) break; 
        }
       
       AppState.addDevice(findUniqueId(a), dataLines, null);
       
       
       
     
        }   
      

    }





  static byte[] convertJpgToBytes(BufferedImage image) throws com.sun.image.codec.jpeg.ImageFormatException, IOException {
  ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(encoderOutputStream);
  encoder.encode(image);
  byte[] resizedImageByteArray = encoderOutputStream.toByteArray();
  return resizedImageByteArray;
  
  }
  
  private static String findUniqueId (String request) {
	 String[] parms = request.split("&");
	 String[] uidparm = parms[0].split("=");
	 return uidparm[1];
  }


}

						  

